package com.hspedu.poly_.polyarr_;

public class PloyArray {
    public static void main(String[] args) {
/*     应用实例:现有一个继承结构如下：
       要求创建 1 个 Person 对象、2 个 Student 对象和 2 个 Teacher 对象,
       统一放在数组中，并调用每个对象
*/
        Person[] persons = new Person[5];
        persons[0] = new Person("jack",20);
        persons[1] = new Student("tom",21,80.8);
        persons[2] = new Student("rose",20,92.2);
        persons[3] = new Teacher("wan",28,20000);
        persons[4] = new Teacher("zhang",50,25000);
        //循环调用
        printArr(persons);
    }

    public static void printArr(Person arr[]){
        for(int i = 0 ; i < arr.length; i++){
            System.out.println("========");
            //编译类型 是Person，运行类型会根据数组中的地址来决定
            //根据实际情况，由JVM机来判断
            System.out.println(arr[i].say());

            if (arr[i] instanceof Student){//判断arr[i]的运行类型是不是Student
                Student student = (Student)arr[i];//向下转型
                student.study();
                //((Student)arr[i]).study(); 也可以这么写，强转之后调用即可
            }else if (arr[i] instanceof Teacher ){
                ((Teacher)arr[i]).teach();
            }else if(arr[i] instanceof Person){
                arr[i].person();
            }else{
                System.out.println("类型有误，请检查");
            }
            System.out.println("========");
        }
    }
}


